package com.ls.thread.ch07;

public class TicketWaitNotity implements Runnable {
	
	private int ticket;
	private boolean flag=true;
	
	public void setTicket(int ticket) {
		this.ticket = ticket;
	}

	public TicketWaitNotity() {
		super();
		
	}

	public TicketWaitNotity(int ticket) {
		super();
		this.ticket = ticket;
	}

	@Override
	public void run() {
		
		//因为不知道具体的线程循环的次数所以用死循环 谁抢到了执行权谁就是jvm运行
		
		while(flag){
			flag=xx();
		}
	}
	
	@SuppressWarnings("static-access")
	public synchronized boolean xx(){
		while(true){
			if(ticket>0){
				
				notify();
				try {
					Thread.currentThread().sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			
				System.out.println(Thread.currentThread().getName()+"售出第"+ticket+"张票");
				ticket--;
				
				try {
					wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				return true;
			}else{
				return false;
			}
			
		}
		
	}

}
